﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SCRUM.Core.BusinessObjects;
using System.Data.Common;

namespace SCRUM.Core.DataProviders
{
    /// <summary>
    /// Database data database (user)
    /// </summary>
    public partial class DBDataProvider : DataProvider
    {
        /// <summary>
        /// Gets the user from DbDataReader
        /// </summary>
        /// <param name="data">The data.</param>
        /// <returns></returns>
        private User GetUser(DbDataReader data)
        {
            return new User(
                GetIntByColumn(data, "ID"),
                GetStringByColumn(data, "LOGIN"),
                GetStringByColumn(data, "PASSWORD"),
                GetStringByColumn(data, "FULLNAME"),
                GetStringByColumn(data, "EMAIL"),
                GetBoolByColumn(data, "ENABLED"),
                GetStringByColumn(data, "TELEPHONE"),
                GetStringByColumn(data, "IM"),
                GetStringByColumn(data, "DESCRIPTION"),
                GetIntByColumn(data, "DEFAULT_ROLE_ID"),
                GetIntByColumn(data, "VELOCITY")
            );
        }

        /// <summary>
        /// Inserts the user.
        /// </summary>
        /// <param name="user">The specific user.</param>
        public override void InsertUser(User user)
        {
        }

        /// <summary>
        /// Updates the user.
        /// </summary>
        /// <param name="user">The specific user.</param>
        public override void UpdateUser(User user)
        {
        }

        /// <summary>
        /// Deletes the user.
        /// </summary>
        /// <param name="user">The specific user.</param>
        public override void DeleteUser(User user)
        {
        }

        // LUCAS
        /// <summary>
        /// Change user's fullname
        /// </summary>
        /// <param name="fullName">new fullname</param>
        /// <returns>true - if change was successfull</returns>
        public override bool ChangeFullName(string login, string fullName)
        {
            if (string.IsNullOrEmpty(fullName))
                return false;

            DbParameter dbLogin = CreateParameter("login", login);
            DbParameter dbFullName = CreateParameter("fullname", fullName);
            int success;

            try
            {
                connection.Open();
                success = ExecuteNonQuery("user/SetFullName", dbLogin, dbFullName);
                return success != 0;
            }
            finally
            {
                connection.Close();
            }
        }

        /// <summary>
        /// Gets users in specified team and role.
        /// </summary>
        /// <param name="team">The team.</param>
        /// <param name="role">The role.</param>
        /// <returns>
        /// List of users in specified team and role.
        /// </returns>
        public override IList<User> GetTeamMembers(Team team, Role role)
        {
            IList<User> users = new List<User>();
            try
            {
                int teamID = team == null ? -1 : team.ID;
                int roleID = role == null ? -1 : role.ID;

                DbParameter dbTeamID = CreateParameter("teamID", teamID);
                DbParameter dbTroleID = CreateParameter("roleID", roleID);

                connection.Open();
                DbDataReader data = GetVector("user/GetTeamMembers", dbTeamID, dbTroleID);

                while (data.Read())
                {
                    User user = GetUser(data);
                    users.Add(user);
                }
            }
            finally
            {
                connection.Close();
            }
            return users;
        }

        /// <summary>
        /// Gets users associated to the task.
        /// </summary>
        /// <param name="task">The task.</param>
        /// <returns>List of tasks.</returns>
        public override IList<User> GetTeamMembers(Task task)
        {
            List<User> users = new List<User>();
            try
            {
                connection.Open();

                DbParameter taskID = CreateParameter("taskID", task.ID);

                DbDataReader data = GetVector("user/GetUsersByTask", taskID);

                while (data.Read())
                {
                    User user = GetUser(data);
                    users.Add(user);
                }
            }
            finally
            {
                connection.Close();
            }
            return users;
        }

        /// <summary>
        /// Gets the users which can be assigned to the task.
        /// </summary>
        /// <param name="team">The team.</param>
        /// <param name="task">The task.</param>
        /// <returns></returns>
        public override IList<User> GetUsersExceptTaskUsers(Team team, Task task)
        {
            List<User> users = new List<User>();
            try
            {
                connection.Open();

                DbParameter teamID = CreateParameter("teamID", team.ID);
                DbParameter taskID = CreateParameter("taskID", task.ID);

                DbDataReader data = GetVector("user/GetUsersExceptTaskUsers", teamID, taskID);

                while (data.Read())
                {
                    User user = GetUser(data);
                    users.Add(user);
                }
            }
            finally
            {
                connection.Close();
            }
            return users;
        }

        /// <summary>
        /// Gets user by his login.
        /// </summary>
        /// <param name="login">The login.</param>
        /// <returns>User with specified login name</returns>
        public override User GetUserByLogin(string login)
        {
            User user = null;
            IList<User> users = GetUsers(-1, login, null, null, null, null, null, -1);
            if (users.Count > 0) user = users[0];
            return user;
        }

        /// <summary>
        /// Gets the users by all specified parameters. if any of parameters is -1 or null it will not be used in search
        /// </summary>
        /// <param name="id">The id.</param>
        /// <param name="login">The login.</param>
        /// <param name="fullname">The fullname.</param>
        /// <param name="email">The email.</param>
        /// <param name="enabled">if set to <c>true</c> [enabled].</param>
        /// <param name="telephone">The telephone.</param>
        /// <param name="im">The im.</param>
        /// <param name="defaultRoleID">The default role ID.</param>
        /// <returns>collection of Users</returns>
        public override IList<User> GetUsers(int id, string login, string fullname,
            string email, bool? enabled, string telephone, string im, int defaultRoleID)
        {
            IList<User> users = new List<User>();
            try
            {
                // bool showAllUsers = false;
                DbParameter dbUserID = CreateParameter("ID", id);
                DbParameter dbLogin = CreateParameter("LOGIN", login);
                DbParameter dbFullName = CreateParameter("FULLNAME", fullname);
                DbParameter dbEmail = CreateParameter("EMAIL", email);
                DbParameter dbEnabled = CreateParameter("ENABLED", enabled);
                DbParameter dbTelephone = CreateParameter("TELEPHONE", telephone);
                DbParameter dbIm = CreateParameter("IM", im);
                DbParameter dbDefaultRoleID = CreateParameter("DEFAULT_ROLE_ID", defaultRoleID);
                //DbParameter dbshowAllUsers = CreateParameter("SHOWALLUSERS", showAllUsers);

                connection.Open();

                DbDataReader data = GetVector("user/GetUsers", dbUserID, dbLogin, dbFullName, dbEmail, dbEnabled, dbTelephone, dbIm, dbDefaultRoleID);

                while (data.Read())
                {
                    User user = GetUser(data);
                    users.Add(user);
                }
            }
            finally
            {
                connection.Close();
            }
            return users;
        }
    }
}
